<?php

namespace app\ask\controller;

use think\Controller;

class Comment extends Controller
{
  public function __construct()
  {
    parent::__construct();

    $this->BusinessModel = model('Business.Business');
    $this->PostModel = model('Post.Post');
    $this->CommentModel = model('Post.Comment');
  }

  // 评论页
  public function index()
  {
    // 判断是否有post请求
    if ($this->request->isPost()) {
      // 接收参数
      $postid = $this->request->param('postid', 0, 'trim');
      $pid = $this->request->param('pid', 0, 'trim');

      // 查找帖子表是否存在
      $post = $this->PostModel->find($postid);

      if (!$post) {
        $this->error('暂无帖子信息');
        exit;
      }

      // 查询评论表中的顶级的评论（并且按照上级评论id、等级、评论主键id排序）
      $top = $this->CommentModel
        ->with(['business'])
        ->where(['postid' => $postid, 'pid' => $pid])
        ->order(['pid asc', 'id asc'])
        ->select();

      // 判断评论是否为空
      if (empty($top)) {
        $this->error('暂无评论');
        exit;
      }

      //循环顶级
      foreach ($top as $key => &$item) {
        // 追加一个children字段，查询出每一个顶级id下面的评论
        // sublist：在模型中封装的一个递归查询方法
        $item['children'] = $this->CommentModel->sublist($item['id']);
      }

      // 返回评论的所有数据
      $this->success('返回评论列表', null, $top);
      exit;
    }
  }

  // 我来回答（评论）
  public function add()
  {
    if ($this->request->isPost()) {
      $pid = $this->request->param('pid', 0, 'trim');
      $content = $this->request->param('content', '', 'trim');
      $postid = $this->request->param('postid', 0, 'trim');
      $busid = $this->request->param('busid', 0, 'trim');

      $post = $this->PostModel->find($postid);

      if (!$post) {
        $this->error('暂无帖子信息');
        exit;
      }

      $business = $this->BusinessModel->find($busid);

      if (!$business) {
        $this->error('用户不存在');
        exit;
      }

      //组装数据
      $data = [
        'pid' => $pid,
        'content' => $content,
        'busid' => $busid,
        'postid' => $postid,
        'status' => '0',
      ];

      $result = $this->CommentModel->save($data);

      if ($result === FALSE) {
        $this->error('评论失败');
        exit;
      } else {
        $this->success('评论成功');
        exit;
      }
    }
  }

  // 删除评论
  public function delete()
  {
    if ($this->request->isPost()) {
      $comid = $this->request->param('comid', 0, 'trim');
      $postid = $this->request->param('postid', 0, 'trim');
      $busid = $this->request->param('busid', 0, 'trim');

      $post = $this->PostModel->find($postid);

      if (!$post) {
        $this->error('暂无帖子信息');
        exit;
      }

      $business = $this->BusinessModel->find($busid);

      if (!$business) {
        $this->error('用户不存在');
        exit;
      }

      // 递归查询
      $tree = $this->CommentModel->subtree($comid);

      // 删除语句
      $result = $this->CommentModel->where(['id' => ['IN', $tree]])->delete();

      if ($result === FALSE) {
        $this->error('删除评论失败');
        exit;
      } else {
        $this->success('删除评论成功');
        exit;
      }
    }
  }
}
